package com.example.javaeefinal.utils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Auther: Lai Wenhao
 * @Date: 2023/12/29 12:58
 **/
public class Interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getSession() != null && request.getSession().getAttribute("User") != null) {
            return true;
        } else {
            request.setAttribute("msg", "尚未登陆");
            //查看url
            StringBuffer url = request.getRequestURL();
            System.out.println("当前 URL 是：" + url.toString());
            //请求转发到登录界面
            request.getRequestDispatcher("/user_sign_in").forward(request, response);
            return false;
        }
    }
}
